"use strict";

angular.module('app.yksensor').controller('YKDeviceCtrl', function ($scope,$http,$cookieStore,$rootScope) {

    //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [10, 20, 30, 40, 50],

        onChange: function () {
        }
    };
    //获得所有项目
    $http.get(baseurls+"/list_nopage/project").then(function(res1){
        // console.log(res1.data)
        if(res1.data.ret == 1){
            $scope.projects = res1.data.info;
        }
    });
    //设备类型
    $http.get(baseurls+"/device_types").then(function(res){
        // console.log(res.data)
        if(res.data.ret == 1){
            $scope.types = res.data.info;
        }
    });
    //1.设备列表
    $scope.getdeviceInfo = function(){
        // $http.get(baseurls+"/list_nopage/device?fid="+con).then(function(res){
        //     if(res.data.ret == 1){
        //         // console.log(res.data.info);
        //         $scope.datas = res.data.info;
        //         $scope.paginationConf.totalItems = res.data.info.length;
        //         $scope.devices = res.data.info;//设备绑定中的所有设备
        //     }
        // })
        $http.post(baseurls+"/list/select/view_device_project", $scope.paginationConf).then(function(res){
            // console.log(res.data)
            if(res.data.ret == 1){
                $scope.datas = res.data.rows;
				$scope.paginationConf.totalItems = res.data.records;
                $scope.devices = res.data.rows;//设备绑定中的所有设备
                
            }
            // console.log($scope.devices)
        })
    }
    $scope.getdeviceInfo();
    //2.添加设备
    $scope.insert = {};
    $scope.insertdata = function () {
        $scope.insert.fid = $cookieStore.get("user");
        $scope.insert.status = 1;//设备状态 正常 0 为报警
        $http.post(baseurls+"/data/device",$scope.insert).then(function(res){
            // console.log(res.data)
            if(res.data.ret == 1){
                // console.log($scope.insert);
                // console.log(res.data.info)
                for(var i=0;i<$scope.devices.length;i++){
                    if($scope.insert.projectid == $scope.devices[i].projectid){
                        var procode = $scope.devices[i].procode;
                    }
                }
                // console.log($scope.insert.procode)
                //设备添加post至云仓
                $scope.postdevice = {
                    "type": "EQUIPMENT_INFO",
                    "code": procode,//仓库代码
                    "downtime": getNowFormatDate(),//下发时间
                    "busicode": getBusicode(),//年月日时分秒加6位随机数
                    "data": {
                        "equipment_code": $scope.insert.deviceid,//设备编号
                        "equipment_name": $scope.insert.devicename,//设备名称
                        "equipment_type": $scope.insert.type == 1 ? "流量计" : ($scope.insert.type == 2 ? "液位计" : ($scope.insert.type == 3 ? "温度计" : "其他")),//设备类型
                        "status": $scope.insert.status_tag,//设备状态:生效 失效
                        "remarks": $scope.insert.zone//备注
                    }
                };
                // console.log($scope.postdevice);
                $http.post("http://120.27.18.161:3001/api/system/device",$scope.postdevice,{withCredentials:false});
                new Notify('success','添加设备成功');
            }else if(res.data.ret != -4){
                new Notify('fail','添加设备失败');
            }
            $("#addModal").modal("hide");
            $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.getdeviceInfo);//更新列表
        })
       
        
       
        
    }

    //3.编辑设备
    $scope.editdata = {};
    $scope.showEditDevice = function (data) {
        // console.log(data)
        $scope.editdata.id = data.id;
        $scope.editdata.clientname = data.clientname;
        $scope.editdata.devicename = data.devicename;
        $scope.editdata.deviceid = data.deviceid;
        $scope.editdata.port = data.port;
        $scope.editdata.type = data.type.toString();
        $scope.editdata.projectid = data.projectid.toString();
        $scope.editdata.sim = data.sim;
        $scope.editdata.zone = data.zone;
        $scope.editdata.pos = data.pos;
        $scope.editdata.status_tag = data.status_tag;
        // $scope.editdata.register = data.register;
        // $scope.editdata.lockdevice = data.lockdevice;

        $('#editModal').modal('show');
    }
    $scope.updatedata = function () {
        $http.put(baseurls+"/data/device?id=="+$scope.editdata.id,$scope.editdata).then(function(res){
            // console.log(res.data)
            if(res.data.ret == 1){
                for(var i=0;i<$scope.devices.length;i++){
                    if($scope.editdata.projectid == $scope.devices[i].projectid){
                        var procode = $scope.devices[i].procode;
                    }
                }
                // console.log($scope.procode)
                $scope.editdevice = {
                    "type": "EQUIPMENT_INFO",
                    "code": procode,//仓库代码
                    "downtime": getNowFormatDate(),//下发时间
                    "busicode": getBusicode(),//年月日时分秒加6位随机数
                    "data": {
                        "equipment_code": $scope.editdata.deviceid,//设备编号
                        "equipment_name": $scope.editdata.devicename,//设备名称
                        "equipment_type": $scope.editdata.type == 1 ? "流量计" : ($scope.editdata.type == 2 ? "液位计" : ($scope.editdata.type == 3 ? "温度计" : "其他")),//设备类型
                        "status": $scope.editdata.status_tag,//设备状态:生效 失效
                        "remarks": $scope.editdata.zone//备注
                    }
                };
                // console.log(data.type);
                // console.log($scope.editdevice);
                //设备编辑post至云仓
                $http.post("http://120.27.18.161:3001/api/system/device",$scope.editdevice,{withCredentials:false});
                new Notify('success','设备信息修改成功');
            }else if(res.data.ret != -4){
                new Notify('fail','设备信息修改失败');
            }
            $("#editModal").modal("hide");
            $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.getdeviceInfo);
        })
       
    }
    //4.删除设备
    $scope.delid = {};
    $scope.showDelDialog = function (data) {
        $scope.delid = data.id;
        
        $scope.deletedevice = {
            "type": "EQUIPMENT_INFO",
            "code": data.procode,//仓库代码
            "downtime": getNowFormatDate(),//下发时间
            "busicode": getBusicode(),//年月日时分秒加6位随机数
            "data": {
                "equipment_code": data.deviceid,//设备编号
                "equipment_name": data.devicename,//设备名称
                "equipment_type": data.type == 1 ? "流量计" : (data.type == 2 ? "液位计" : (data.type == 3 ? "温度计" : "其他")),//设备类型
                "status": "失效",//设备状态:生效 失效
                "remarks": data.zone//备注
            }
        };
        $('#showDelDialog').modal('show');
    }
    $scope.deldevice = function () {
        $http.delete(baseurls+"/data/device?id="+$scope.delid).then(function(res){
            if(res.data.ret == 1){
                // console.log($scope.deletedevice);
                $http.post("http://120.27.18.161:3001/api/system/device",$scope.deletedevice,{withCredentials:false});
                new Notify('success','删除设备成功');
            }else if(res.data.ret != -4){
                new Notify('fail','删除设备失败');
            }
            $('#showDelDialog').modal('hide');
            $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.getdeviceInfo);
        })
               
    }
    //5.设备与项目绑定

    $scope.bindObj = {
        did: "0",
        pid: "0",
    };
    $scope.bind_dialog = function(){
        $("#bind").modal("show");
    }
    $scope.bind = function(){
        $http.put(baseurls+"/data/device?id=="+$scope.bindObj.did,{projectid: $scope.bindObj.pid}).then(function(res){
            if(res.data.ret == 1){
                new Notify('success','设备与项目绑定成功');
            }else if(res.data.ret != -4){
                new Notify('fail','设备与项目绑定失败');
            }
            console.log('将设备'+$scope.bindObj.did+'与项目'+$scope.bindObj.pid+'绑定');
            $("#bind").modal("hide");
            $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.getdeviceInfo);
        })
    }
    //6.参数设置
    $scope.data = {};
    $scope.parameters = {};
    $scope.showSetDialog = function (item) {
        if (item.press_unit)
            $scope.unit = item.press_unit;
        else
            $scope.unit = "Pa";
        $scope.parameters.id = item.id;
        $scope.parameters.deviceid = item.deviceid;
        if (item.warn_up)
            $scope.parameters.warn_up = Number(item.warn_up);
        else
            $scope.parameters.warn_up = 0;

        if (item.warn_down)
            $scope.parameters.warn_down = Number(item.warn_down);
        else
            $scope.parameters.warn_down = 0;

        if (item.col_cycle)
            $scope.parameters.col_cycle = Number(item.col_cycle);
        else
            $scope.parameters.col_cycle = 0;
        
        $scope.parametercheck.col_cycle = false;
        $scope.parametercheck.warn_up = false;
        $scope.parametercheck.warn_down = false;
        $('#setDialog').modal('show');
    }
    $scope.parametercheck = {};
    $scope.sendparameter = {};
    $scope.setParameters = function () {

        if ($scope.parametercheck.col_cycle) {
            if ($scope.parameters.col_cycle)
                $scope.sendparameter.col_cycle = $scope.parameters.col_cycle;
        };
        if ($scope.parametercheck.warn_up) {
            if ($scope.parameters.warn_up)
                $scope.sendparameter.warn_up = $scope.parameters.warn_up;
        };
        if ($scope.parametercheck.warn_down) {
            if ($scope.parameters.warn_down)
                $scope.sendparameter.warn_down = $scope.parameters.warn_down;
        };

        $scope.sendparameter.deviceid = $scope.parameters.deviceid
        console.log($scope.sendparameter);
        if($scope.parametercheck.col_cycle || $scope.parametercheck.warn_up || $scope.parametercheck.warn_down){
			$http.put(baseurls+"/edit_device?id=="+$scope.parameters.id+"&deviceid=="+$scope.parameters.deviceid,$scope.sendparameter).then(function(res){
				// console.log($scope.parameters.id,$scope.parameters.deviceid,$scope.sendparameter)
				// console.log(res)
				if(res.data.ret == 1){
					new Notify('success','发送成功');
				}else if(res.data.ret != -4){
					new Notify('fail','发送失败');
				}
				$('#setDialog').modal('hide');
				$scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.getdeviceInfo);
			})
		}else{
			new Notify('fail','没有更改，更改项请勾选对应复选框');
		}
    }

    //7.下发查询
	$scope.querydata = {};
	$scope.showQueryDialog = function (deviceid) {
	    // console.log(deviceid);
		$scope.querydata = {deviceid: deviceid};
		// $http.post(baseurls+'/query_device', $scope.querydata).then(function(res){
        //     // console.log(res);
        //     if(res.data.ret == 1){
		// 		if(res.data.info == null){
		// 			new Notify('fail','查询到的数据为空');
		// 		}else{
		// 			$scope.realdata = res.data.info;
		// 		}
		// 	}
        // });
		$http.get(baseurls+"/detail/device?deviceid=="+$scope.querydata.deviceid).then(function(res){
			// console.log(res.data);
			if(res.data.ret == 1){
				$scope.realdata = res.data.info;
			}
		})

	    $('#queryDialog').modal('show');
	}

	$scope.querydevice = function ()
	{
		$http.post(baseurls+'/query_device', $scope.querydata).then(function(res){
            // console.log(res);
            if(res.data.ret == 1){
				if(res.data.info == null){
					new Notify('fail','查询到的数据为空');
				}else{
					$scope.realdata = res.data.info;
				}
			}
        });
		// $http.get(baseurls+"/detail/device?deviceid=="+$scope.querydeviceid).then(function(res){
		// 	console.log(res.data);
		// 	if(res.data.ret == 1){
		// 		$scope.realdata = res.data.info;
		// 	}
		// })
	}
    
});
